<?php
/**
 * Created by PhpStorm.
 * User: hjc_0
 * Date: 2016/11/30
 * Time: 22:57
 */

class UtilitiesOauthInterf
{
    public $accountId;
    public $user_utilities_id;
    public $old_room_code;
    public $new_room_code;
    public $room_code;
    public $new_householder_id;
    public $old_householder_id;
    public $paymentInfo;

    public function callOauthIntf()
    {
        $apiIP = C('zywyapi.api_ip');
        $client = new SoapClient($apiIP."/WebService.asmx?wsdl");
       $re = $client->searchAccount();
       return $re;
        $params = array('area' =>'123','block'=>'122' ,'chamber'=>'123');
        $strJson = $client->__soapCall('searchAccount', array('parameters' => $params));
        var_dump($strJson);
        die();
        $callResArr = json_decode($strJson->searchAccountResult, true);
        return $callResArr;

    }

    /**
     * 验证sign和req_time
     * 两个对象间复制属性值 ，当一个属性在两边都存在时才复制
     * 属性值是NULL也会复制
     * @param mixed $parm 数据源，数组|对象
     * @return Object 返回对象本身
     */
    public function setProperties($parm)
    {
        $client_secret = 'adfasdfsad';//测试秘钥
        $data = array();

        if(!$parm['req_time'] || !$parm['sign'] || strlen($parm['sign']) !== 32 || $parm['req_time']>time()){
            return $this->_returnResult(10000,'参数错误');
        }

        if(time() - $parm['req_time'] > 5){
            return $this->_returnResult(10003,'超时');
        }

        foreach ($parm as $k =>$v) {
            if(!in_array($k,array('sign','req_time'))){
                $data[$k] = $v;
            }
        }

        $sign = $this->_getSign($client_secret,$data);

        if(!$sign || $sign !== $parm['sign']){
            return $this->_returnResult(10004,'签名错误');
        }

        $src_vars = is_object($parm) ? get_object_vars($parm) : $parm;
        foreach ($src_vars as $name => $value) {
            if (property_exists($this, $name)) {
                $this->$name = $value;
            }
        }
        return $this;
    }

    /**
     * 物业学生换寝接口
     * @return bool
     * @Auth    : piyechao
     * @Now     :2017/8/2 16:39
     */
    public function userSwopRoom()
    {
       if(!$this->user_utilities_id || !$this->old_room_code || !$this->new_room_code){
           return $this->_returnResult(10000,'参数错误');
       }
       $condition_user = array('user_utilities_id' => $this->user_utilities_id,'room_code' => $this->old_room_code);
       $user = M('UtilitiesUser') ->where($condition_user) ->find();
       if(!$user){
           return $this->_returnResult(10000,'参数错误');
       }
       $condition_room = array('room_code'=>$this->new_room_code);
       $room = M('Room') ->where($condition_room) ->find();
       if(!$room){
           return $this->_returnResult(10000,'参数错误');
       }
       $data['room_code'] = $this->new_room_code;
       $data['check_time'] = time();
       $data['user_type'] = 2;
       $re = M('UtilitiesUser') ->where($condition_user) ->save($data);
//       $condition_user_info = array('user_id'=>$user['user_id']);
//       $data = Service('Utilities')->getAddressByRoomUtilitiesId($this->new_room_code);
//       if(!is_array($data)){
//           return $this->_returnResult(10002,$data);
//       }
//       $res = M('UserInfo')->where($condition_user_info)->save($data);
       if(!$re){
           return $this->_returnResult(10001,'换寝失败');
       }

       return $this->_returnResult(200,'成功');

    }

    /**
     * 更换户主接口
     * @return bool
     * @Auth    : piyechao
     * @Now     :2017/8/2 16:49
     */
    public function roomSwopUser()
    {
        if(!$this->room_code || !$this->old_householder_id || !$this->new_householder_id) {
            $this->_returnResult(10000, '参数错误');
        }
        $condition_old_user = array('user_utilities_id' => $this->old_householder_id, 'room_code' => $this->room_code);
        $condition_new_user = array('user_utilities_id' => $this->new_householder_id);
        $old_householder = M('UtilitiesUser')->where($condition_old_user)->find();
        $new_householder = M('UtilitiesUser')->where($condition_new_user)->find();
        if (!$old_householder || !$new_householder) {
            return $this->_returnResult(10000, '参数错误');
        }
        $condition_room = array('room_code' => $this->room_code, 'householder' => $old_householder['user_id']);
        $room = M('Room')->where($condition_room)->find();
        if (!$room) {
            return $this->_returnResult(10000, '参数错误');
        }

        $data_room = array('householder_id' => $new_householder['user_id'], 'room_id' => $room['room_id']);
        $re1 = M('Room')->update($data_room);

        $data_old_room = array(
            'user_type' => 2,
            'check_time' => time(),
            'id' => $old_householder['id'],
        );
        $re2 = M('UtilitiesUser')->update($data_old_room);

        $data_new_room = array(
            'user_type' => 3,
            'room_code' => $this->room_code,
            'id' => $new_householder['id'],
        );
        $re3 = M('UtilitiesUser')->update($data_new_room);

        if ($re1 && $re2 && $re3) {
            return $this->_returnResult(200, '成功');
        } else {
            return $this->_returnResult(10001, '更换户主失败');
        }

    }

    /**
     * 接收欠费信息
     * @return array
     * @Auth    : piyechao
     * @Now     :2017/8/9 14:33
     */
    public function pushNotification()
    {
        $res = array();
        if(!$this->paymentInfo || !is_array($this->paymentInfo)){
            return $this->_returnResult(10000,'参数错误');
        }
        foreach ($this->paymentInfo as $k => &$v) {
            //验证字段
            if(!in_array($k , array('room_code','total_fee','type')) || is_string($v['room_code']) || !is_int($v['type']) || !is_numeric($v['total_fee'])){
//                return $this->_returnResult(10000,'参数错误');
                continue;
            }
            $data = array();
            if($v['type'] == 1){
                $data['balance_cold_water'] = -abs($v['total_fee']);
            }elseif($v['type'] == 2){
                $data['balance_cold_water'] = -abs($v['total_fee']);
            }elseif($v['type'] == 3){
                $data['balance_cold_water'] = -abs($v['total_fee']);
            }else{
                continue;
            }
            $data['update_time'] = time();

            $condition = array('room_code'=>$v['room_code'] ,'account_id'=>$this->accountId ,'cut_off'=>0);
            $re = M('UtilitiesBalance')->where($condition)->find();
            if($re){
                $res[] = M('UtilitiesBalance')->where($condition)->save($data);
            }else{
                $data['account_id'] = $this->accountId;
                $data['room_code'] = $v['room_code'];
                $res[] = M('UtilitiesBalance')->add($data);
            }
        }

        if(!in_array(false , $res)){
            $this->_returnResult(200,'接收成功');
        }else{
            $this->_returnResult(10001,'接收失败');
        }

    }

    /**
     * 获得sign 签名
     * @param $client_secret
     * @param array $data
     * @return string
     * @Auth    : piyechao
     * @Now     :2017/8/10 10:18
     */
    private function _getSign($client_secret,$data=array()){
        if(!is_string($client_secret)){
            $this->_returnResult(10000,$client_secret.'参数类型错误');
        }
        if(!is_array($data)){
            $this->_returnResult(10000,$data.'参数类型错误');
        }
        $array=ksort($data);
        $NewData='';
        foreach($array as $key=>$v){
            $NewData.=$key.$v;
        }
        $str=md5($client_secret.md5($client_secret.$NewData.$client_secret));
        $code=strtoupper($str);
        return $code;

    }

    /**
     * 接口返回格式
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return array
     * @Auth    : piyechao
     * @Now     :2017/8/25 10:07
     */
    private function _returnResult($code = 10000 ,$msg = '参数错误' ,$data = array())
    {
        $arr = array(
            'ret_code' => $code,
            'ret_msg' => $msg,
        );
        if($data){
            $arr['ret_data'] = $data;
        }
        return $arr;
    }

}